import json

from flask import Blueprint, request, jsonify
from application.api.schemas import *

new_api = Blueprint('new_api', __name__)


# 获取角色列表
@new_api.route('/role', methods=['GET'])
def get_all_role():
    get_roles = Role.query.all()
    role_schema = RoleSchema(many=True)
    roles = role_schema.dump(get_roles)
    return jsonify({'status_code': 200, 'status_message': 'successful', 'data': roles})


# 角色详情
@new_api.route('/role/<int:role_id>', methods=['GET'])
def get_role_by_id(role_id):
    get_role = Role.query.get(role_id)
    role_schema = RoleSchema()
    role = role_schema.dump(get_role)
    return jsonify({'status_code': 200, 'status_message': 'successful', 'data': role})


# 增加角色
@new_api.route('/role', methods=['POST'])
def create_role():
    json_data_str = request.get_data(as_text=True)
    json_data = json.loads(json_data_str)
    role_schema = RoleSchema()
    role = role_schema.load(json_data)
    db.session.add(role)
    db.session.commit()
    role = role_schema.dump(role)
    return jsonify({'status_code': 200, 'status_message': 'successful', 'data': role})


# 修改角色
@new_api.route('/role/<int:role_id>', methods=['PUT'])
def update_role(role_id):
    json_data_str = request.get_data(as_text=True)
    json_data = json.loads(json_data_str)

    get_role = Role.query.get(role_id)
    if 'name' in json_data:
        get_role.name = json_data['name']
    if 'code' in json_data:
        get_role.code = json_data['code']

    db.session.add(get_role)
    db.session.commit()

    role_schema = RoleSchema()
    role = role_schema.dump(get_role)
    return jsonify({'status_code': 200, 'status_message': 'successful', 'data': role})


# 删除角色
@new_api.route('/role/<int:role_id>', methods=['DELETE'])
def delete_role(role_id):
    get_role = Role.query.get(role_id)
    db.session.delete(get_role)
    db.session.commit()
    return jsonify({'status_code': 200, 'status_message': '成功', 'id': get_role.id})


# 获取用户拥有的角色
@new_api.route('/user/role/<int:user_id>', methods=['GET'])
def get_user_role(user_id):
    get_roles = Role.query.filter(Role.id.in_(UsersRole.query.with_entities(UsersRole.role_id).filter(UsersRole.user_id == user_id))).with_entities(Role.id, Role.name, Role.code).all()
    role_schema = RoleSchema(many=True)
    roles = role_schema.dump(get_roles)
    return jsonify({'status_code': 200, 'status_message': '成功', 'data': roles})


# 修改用户拥有的角色
@new_api.route('/user/role/<int:user_id>', methods=['POST'])
def update_user_role(user_id):
    json_data_str = request.get_data(as_text=True)
    json_data = json.loads(json_data_str)

    demand = json_data['demand']
    role_id = json_data['role_id']

    if demand == 'add':
        record = UsersRole()
        record.user_id = user_id
        record.role_id = role_id
        db.session.add(record)
        db.session.commit()
    elif demand == 'delete':
        record = UsersRole.query.filter_by(user_id=user_id, role_id=role_id).first()
        db.session.delete(record)
        db.session.commit()
    get_roles = Role.query.filter(Role.id.in_(UsersRole.query.with_entities(UsersRole.role_id).filter(UsersRole.user_id == user_id))).with_entities(Role.id, Role.name, Role.code).all()
    role_schema = RoleSchema(many=True)
    roles = role_schema.dump(get_roles)
    return jsonify({'status_code': 200, 'status_message': '成功', 'data': roles})



# 获取权限列表
@new_api.route('/permission', methods=['GET'])
def get_permissions():
    get_permission = Permission.query.all()
    permission_schema = PermissionSchema(many=True)
    permissions = permission_schema.dump(get_permission)
    return jsonify({'status_code': 200, 'status_message': '成功', 'data': permissions})


# 获取角色权限
@new_api.route('/role/permission/<int:role_id>', methods=['GET'])
def get_role_permission(role_id):
    get_user_permission = Permission.query.filter(Permission.id.in_(RolePermission.query.with_entities(RolePermission.permission_id).filter(RolePermission.role_id == role_id))).with_entities(Permission.id, Permission.name, Permission.code).all()
    permission_schema = PermissionSchema(many=True)
    permission = permission_schema.dump(get_user_permission)
    return jsonify({'status_code': 200, 'status_message': '成功', 'data': permission})


# 修改角色权限
@new_api.route('/role/permission/<int:role_id>', methods=['POST'])
def update_role_permission(role_id):
    json_data_str = request.get_data(as_text=True)
    json_data = json.loads(json_data_str)

    demand = json_data['demand']
    permission_id = json_data['permission_id']

    if demand == 'add':
        record = RolePermission()
        record.permission_id = permission_id
        record.role_id = role_id
        db.session.add(record)
        db.session.commit()
    elif demand == 'delete':
        record = RolePermission.query.filter_by(permission_id=permission_id, role_id=role_id).first()
        db.session.delete(record)
        db.session.commit()
    get_user_permission = Permission.query.filter(Permission.id.in_(RolePermission.query.with_entities(RolePermission.permission_id).filter(RolePermission.role_id == role_id))).with_entities(Permission.id, Permission.name, Permission.code).all()
    permission_schema = PermissionSchema(many=True)
    permission = permission_schema.dump(get_user_permission)
    return jsonify({'status_code': 200, 'status_message': '成功', 'data': permission})
